package com.daylightrobbery.server.packet.game.common;

import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;

public class ChanceCardReader {

    public static final String filePath = "chanceCardData.txt";

    public static HashMap<Integer, ChanceCardData> read(){
        ArrayList<Integer> indexes = new ArrayList<>();
        ArrayList<String> msgs = new ArrayList<>();
        ArrayList<Boolean> playerGets = new ArrayList<>();
        ArrayList<Integer> moneys = new ArrayList<>();

        ClassPathResource resource = new ClassPathResource(filePath);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 假设每个值之间使用逗号分隔
                String[] parts = line.split(",");
                if (parts.length == 4) { // 确保数据格式正确
                    indexes.add(Integer.parseInt(parts[0]));
                    msgs.add(parts[1]);
                    playerGets.add(parts[2].equals("GET"));
                    moneys.add(Integer.parseInt(parts[3]));
                }else{
                    for (String part : parts) {
                        System.out.println(part);
                        throw new RuntimeException("Error!");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 输出读取的数据，验证结果
        System.out.println("Indexes: " + indexes);
        System.out.println("msgs: " + msgs);
        System.out.println("playerGets: " + playerGets);
        System.out.println("Money: " + moneys);

        HashMap<Integer, ChanceCardData> map = new HashMap<>();

        for (int i = 0; i < indexes.size(); i++) {
            ChanceCardData blockData = new ChanceCardData();
            blockData.setMsg(msgs.get(i))
                    .setPlayerGet(playerGets.get(i))
                    .setMoney(moneys.get(i));
            map.put(indexes.get(i), blockData);
        }
        return map;
    }


}
